const util=require('../../utils/util.js');
const config=require('../../utils/config.js');
let res_name=null;//物品名称
let res_desc=null;//物品描述
let res_imgs=null;//图片链接
let class_list=null;
let edit=null;//编辑标识
let res_data;//编辑内容
Page({
  data: {
    files: [],
    showView: true,
    imgurl: '',
    form_info: '',
    accounts: null,
    accountIndex: 0,
  },
  onLoad: function (options) {
    edit=options.edit;
    if(edit==1){
      res_data=wx.getStorageSync('edit_item');
      var temp_arr=[];
      temp_arr.push(config.root_url + res_data.res_imgs)
      console.log('arr', temp_arr)
      this.setData({
        res_data:res_data,
        files: temp_arr,
        showView: false,
        edit: edit,
        checked: res_data.is_check==0?false:true
      })
      res_imgs = res_data.res_imgs;
      res_name = res_data.res_name;//物品名称
      res_desc = res_data.res_desc;//物品描述
    }
    class_list=wx.getStorageSync('class_list');
    let arr_class=[];
    for(i in class_list){
      arr_class.push(class_list[i].class_name)
      if (edit == 1 && res_data.class_id == class_list[i].class_id){
        this.setData({
          accountIndex:i
        })
      }
    }
    this.setData({
      accounts: arr_class,
      class_list: class_list
    })
  },
  onChangeShowState: function () {//隐藏添加图片按钮
    var that = this;
    that.setData({
      showView:false
    })
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
         
        //console.log(res.tempFilePaths)
        
        var tempFilePaths = res.tempFilePaths
        util.show_loading();
        console.log('path', tempFilePaths[0]);
        let data={
          imgFile: tempFilePaths[0]
        }
        util.upload_file(tempFilePaths[0], data, res => {
          util.hide_loading();
          console.log('upload:',res);
          if(res.ret==0){
            that.onChangeShowState();
            that.setData({
              files: tempFilePaths
            });
            res_imgs=res.url;
          }else{
            wx.showToast({
              title: '图片上传失败！',
              icon:'none'
            })
          }
        });
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  
  closeNav: function () {
    wx.navigateBack({ delta: 2 })
  },
  bindAccountChange:function(e){
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      accountIndex: e.detail.value
    })
  },
  input_name:function(e){
    res_name=e.detail.value;
  },
  input_res_desc:function(e){
    res_desc=e.detail.value;
  },
  submit:function(){
    if (res_name == null || res_desc == null || res_imgs==null){
      wx.showToast({
        title: '请输入物品完整信息并上传图片',
        icon:'none',
        duration:3000
      })
      return;
    }
    let content ='添加物品成功';
    let url ='/app/insert_b_res';
    let data={
      open_id:wx.getStorageSync('open_id'),
      class_id: class_list[this.data.accountIndex].class_id,
      res_name: res_name,
      res_desc: res_desc,
      res_imgs: res_imgs,
      is_check:this.data.checked==true?1:0
    }
    if (edit == 1) {
      url = '/app/update_b_res';
      data.res_id = res_data.res_id;
      content = '修改物品成功'
    }
    // console.log(url,data);
    util.show_loading();
    util.request(url,data,res=>{
      util.hide_loading();
      if(res.ret==0){
        wx.showModal({
          title: '系统提示',
          content: content,
          showCancel:false,
          success:function(e){
            getCurrentPages()[1].onLoad();
            // console.log('currentPage', getCurrentPages())
            wx.navigateBack({})
          }
        })
      }else{
        wx.showModal({
          title: '系统提示',
          content: '操作失败，请稍后再试',
        })
      }
    })
  },
  delete:function(e){
    // let that=this;
    wx.showModal({
      title: '系统提示',
      content: '确定要删除该物品？',
      success:function(res){
        if (res.confirm) {
          let url = '/app/delete_b_res';
          let data = {
            res_id: res_data.res_id
          };
          util.show_loading();
          util.request(url, data, res => {
            util.hide_loading();
            if(res.ret==0){
              wx.showModal({
                title: '系统提示',
                content: '删除成功',
                showCancel: false,
                success: function (e) {
                  wx.navigateBack({})
                }
              })
            }else{
              wx.showModal({
                title: '系统提示',
                content: '删除失败，请稍后再试。',
                showCancel: false,
                success: function (e) {
                }
              })
            }
          })
        } else if (res.cancel) {

        }
      }
    })
  },
  input_check:function(e){
    let checked=e.detail.value;
    this.setData({
      checked:checked
    })
  }
});